package com.sd.view;

import java.util.Vector;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.AdapterView.OnItemClickListener;

import com.sd.activity.R;
import com.sd.reader.ReadAttribute;
import com.sd.reader.ReaderConfig;
import com.sd.reader.ReaderSet;

/**
 * 
 * 
 * @author Alex.Lu
 *
 */
public class ReadsetDialogManager implements OnClickListener, android.view.View.OnClickListener {

	private static ReadsetDialogManager instance;
	private Dialog mDialog;
	
	public static ReadsetDialogManager getInstance(){
		if(instance == null){
			instance = new ReadsetDialogManager();
		}
		return instance;
	}
	
	public void createFontsize(final Context context,OnClickListener listener){
		
		int fontSize = (int)ReaderSet.getInstance().getmFontSize();
		int mIndex = 0;
		final String[] strs = new String[8];
		for(int i=0;i<strs.length;i++){
			strs[i] = 14+2*i+"";
			if(fontSize == 14+2*i){
				mIndex = i;
			}
		}
		
		mDialog = new AlertDialog.Builder(context)
        .setTitle(R.string.str_readset_fontsize)
        .setSingleChoiceItems(strs, mIndex,new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                /* User clicked Yes so do some stuff */
            	ReaderSet.getInstance().setFontSize(Float.valueOf(strs[whichButton]));
            }
        })
        .setNegativeButton(R.string.btn_ok,listener).create();
		mDialog.setCanceledOnTouchOutside(true);
		mDialog.show();
	}
	
	public void createReadModel(final Context context,OnClickListener listener){
		int index = 0;
		int readmodel = ReaderSet.getInstance().getReadmode();
		final Vector<ReadAttribute> readmode = ReaderConfig.READMODE;
		CharSequence[] strs = new CharSequence[readmode.size()];
		for(int i=0;i<readmode.size();i++){
			strs[i]= (CharSequence)readmode.get(i).mValue;
			if(readmodel == readmode.get(i).mKey){
				index = i;
			}
		}
		mDialog = new AlertDialog.Builder(context)
        .setTitle(R.string.str_readset_readmodel)
        .setSingleChoiceItems(strs,index, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                /* User clicked Yes so do some stuff */
            	ReaderSet.getInstance().setReadmode(readmode.get(whichButton).mKey);
            }
        })
        .setNegativeButton(R.string.btn_ok,listener).create();
		mDialog.setCanceledOnTouchOutside(true);
		mDialog.show();
	}
	
	public void createBG(final Context context,android.view.View.OnClickListener listener){
		int index = 0;
		int readbg0 = ReaderSet.getInstance().getReadBg();
		final Vector<ReadAttribute> readbg= ReaderConfig.READBG_MAP;
		CharSequence[] strs = new CharSequence[readbg.size()];
		for(int i=0;i<readbg.size();i++){
			strs[i]= ""+readbg.get(i).mValue;
			if(readbg.get(i).mKey == readbg0){
				index = i;
			}
		}
		
		LayoutInflater mInflater =LayoutInflater.from(context);
		View view = mInflater.inflate(R.layout.dialog_bgs, null);
		GridView bgs = (GridView)view.findViewById(R.id.bgs);
		bgs.setAdapter(new BgAdapter(context, readbg));
		bgs.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				int count = parent.getChildCount();
				for(int i=0;i<count;i++){
					parent.getChildAt(i).findViewById(R.id.bg).setSelected(false);
				}
				view.setSelected(true);
				ReadAttribute attribute = (ReadAttribute)parent.getItemAtPosition(position);
            	ReaderSet.getInstance().setReadBg((Integer)attribute.mValue);
				ReaderSet.getInstance().setFontColor((Integer)attribute.mValue2);
			}
		});
		
		Button btn = (Button)view.findViewById(R.id.btn_ok);
		btn.setOnClickListener(listener);
		mDialog = new Dialog(context);
		mDialog.setTitle(R.string.str_readset_bg);
		mDialog.setContentView(view);
		mDialog.setCanceledOnTouchOutside(true);
		mDialog.show();
	}
	
	public void createFontStyle(final Context context,OnClickListener listener){
		
		int index = 0;
		int fontStyle = ReaderSet.getInstance().getFontStyle();
		final Vector<ReadAttribute> fonStyles = ReaderConfig.READFONTSTYLE;
		CharSequence[] strs = new CharSequence[fonStyles.size()];
		for(int i=0;i<fonStyles.size();i++){
			strs[i]= (CharSequence)fonStyles.get(i).mValue;
			if(fontStyle == fonStyles.get(i).mKey){
				index = i;
			}
		}
		mDialog = new AlertDialog.Builder(context)
        .setTitle(R.string.str_readset_fontsize)
        .setSingleChoiceItems(strs, index, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {

            	ReaderSet.getInstance().setFontStyle(fonStyles.get(whichButton).mKey);
                /* User clicked Yes so do some stuff */
            }
        })
        .setNegativeButton(R.string.btn_ok,listener).create();
		mDialog.setCanceledOnTouchOutside(true);
		mDialog.show();
	}
	
	
	public void createFamily(final Context context,OnClickListener listener){
		
		int index = 0;
		String family = ReaderSet.getInstance().getFontFamily();
		
		final Vector<ReadAttribute> familys = ReaderConfig.READFAMILY;
		CharSequence[] strs = new CharSequence[familys.size()];
		for(int i=0;i<familys.size();i++){
			strs[i]= (CharSequence)familys.get(i).mValue;
			if(family.equals((String)familys.get(i).mValue)){
				index = i;
			}
		}
		mDialog = new AlertDialog.Builder(context)
        .setTitle(R.string.str_readset_family)
        .setSingleChoiceItems(strs, index, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {

                /* User clicked Yes so do some stuff */
            	ReaderSet.getInstance().setFontFamily((String)(familys.get(whichButton).mValue));
            }
        })
        .setNegativeButton(R.string.btn_ok,listener).create();
		mDialog.setCanceledOnTouchOutside(true);
		mDialog.show();
	}
	
	public void dismiss(){
		mDialog.dismiss();
	}

	@Override
	public void onClick(DialogInterface dialog, int which) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		mDialog.dismiss();
	}

	
}
